package cn.com.blackboard.DataModule4B2.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Condition {
	private static final Logger logger = LoggerFactory.getLogger(Condition.class);
	
	public final static String SearchAll = "1=1";
	
	private String attrStr;
	private String operStr;
	private String isImported;
	private String isExported;
	private String value;
	private String extendOperstr;
	private boolean isDefault = false;
	
	public String getAttrStr() {
		return attrStr;
	}

	public void setAttrStr(String attrStr) {
		this.attrStr = attrStr;
	}

	public String getOperStr() {
		return operStr;
	}

	public void setOperStr(String operStr) {
		this.operStr = operStr;
	}

	public String getIsImported() {
		return isImported;
	}

	public void setIsImported(String isImported) {
		this.isImported = isImported;
	}

	public String getIsExported() {
		return isExported;
	}

	public void setIsExported(String isExported) {
		this.isExported = isExported;
	}
	
	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getExtendOperstr() {
		return extendOperstr;
	}

	public void setExtendOperstr(String extendOperstr) {
		this.extendOperstr = extendOperstr;
	}

	public static Condition getDefault(){
		Condition c = new Condition();
		c.isDefault = true;
		return c;
		
	}
	
	public static String RenderConditoin(Condition condition){
		return RenderConditoin(condition, "");
	}
	
	public static String RenderConditoin(Condition condition, String tableName){
		if(condition.isDefault)
			return SearchAll;
		Attribute attribute = Attribute.valueOf(condition.getAttrStr());
		Operator oper = Operator.valueOf(condition.getOperStr());
		String isImported = condition.getIsImported();
		String isExported = condition.getIsExported();
		String value = condition.getValue();
		
		String result = "";
		result = oper.getValue().replace("$", value);
		if(tableName.equals("")||tableName==null){
			result = attribute.getValue()+" "+result;
			if(isImported!=null&&!isImported.equals("")&&!isImported.equalsIgnoreCase("all"))
				result += " and isImported = "+"\'"+isImported+"\'";
			
			else if(isExported!=null&&!isExported.equals("")&&!isExported.equalsIgnoreCase("all"))
				result += " and isExported = "+"\'"+isExported+"\'";
		}
		else{
			result = tableName+"."+attribute.getValue()+" "+result;
			if(isImported!=null&&!isImported.equals("")&&!isImported.equalsIgnoreCase("all"))
				result += " and "+tableName+".isImported = "+"\'"+isImported+"\'";
			else if(isExported!=null&&!isExported.equals("")&&!isExported.equalsIgnoreCase("all"))
				result += " and "+tableName+".isExported = "+"\'"+isExported+"\'";
		}
		return result;
	}
	
	public static Condition ParseCondition(String jsonStr){
		if(jsonStr==null||jsonStr.equals(""))
			return Condition.getDefault();
		JSONObject jsonObj=JSONObject.fromObject(jsonStr);
		Condition condition =(Condition) JSONObject.toBean(jsonObj,Condition.class);
		if(condition.getValue()==null||condition.getValue().equals(""))
			return Condition.getDefault();
		return condition;
	}
	
	public static List<Condition> ParseConditionList(String jsonArrStr){
		List<Condition> result = new ArrayList<Condition>();
		if(jsonArrStr==null||jsonArrStr.equals("")){
			return result;
		}		
		JSONArray jsonArr = JSONArray.fromObject(jsonArrStr);
		for(int i = 0; i < jsonArr.size(); i++){
			Condition condition = new Condition();
			String jsonStr = jsonArr.get(i).toString();
			condition = ParseCondition(jsonStr);
			result.add(condition);
		}
		return result;
	}
	
	public static String RenderConditoinList(List<Condition> conditionList){
		return RenderConditoinList(conditionList, "");
	}
	public static String RenderConditoinList(List<Condition> conditionList, String tableName){
		String result = "";
		if(conditionList.size()==0)
			return SearchAll;
		for(Condition c:conditionList){
			if(!c.isDefault){
				String appender = c.getExtendOperstr()!=null&&!c.getExtendOperstr().equals("")?" "+c.getExtendOperstr()+" ":"";
				result += appender + Condition.RenderConditoin(c, tableName);
			}
		}
		return result;		
	}
	
	public static void main(String[] args) {
		
		String jsonStr = "{\"attrStr\":\"UserId\",\"operStr\":\"Contains\",\"value\":\"12\",\"isExported\":\"\",\"isImported\":\"all\"}";
		
		
		String jsonStr1 = "{\"attrStr\":\"FirstName\",\"operStr\":\"Contains\",\"value\":\"34\",\"extendOperstr\":\"and\"}";
		
		//String jsonArr = "["+jsonStr+","+jsonStr1+"]";
		String jsonArr = "[{\"attrStr\":\"UserId\",\"operStr\":\"Contains\",\"value\":\"20\",\"isExported\":\"\",\"isImported\":\"all\"},{\"attrStr\":\"DepartmentID\",\"operStr\":\"Contains\",\"value\":\"1001\",\"extendOperstr\":\"and\"},{\"attrStr\":\"UserId\",\"operStr\":\"Contains\",\"value\":\"\",\"extendOperstr\":\"and\"}]";
		logger.info(jsonArr);
		
		List<Condition>conditionList= ParseConditionList(jsonArr);
		String conditionStr2 = Condition.RenderConditoinList(conditionList);
		System.out.println(conditionStr2);
		
	}
	
	public static enum Attribute {
		UserId("USER_ID"),
		FirstName("FIRST_NAME"),
		EMAIL("EMAIL"),
		PASSWORD("PASSWORD"),
		DepartmentID("DEPARTMENT_ID"),
		PK1("PK1"),
		COURSE_ID("COURSE_ID"),
		DEPARTMENT_NAME("DEPARTMENT_NAME"),
		REGULAR_TEST("REGULAR_TEST"),
		MID_TERM_TEST("MID_TERM_TEST"),
		FINAL_TEST("FINAL_TEST"),
		DTCREATED("DTCREATED"),
		COURSE_NAME("COURSE_NAME"),
		ATTRIBUTE1("ATTRIBUTE1"),
		ATTRIBUTE2("ATTRIBUTE2"),
		ATTRIBUTE3("ATTRIBUTE3"),
		ATTRIBUTE4("ATTRIBUTE4"),
		ATTRIBUTE5("ATTRIBUTE5");
		
		private String value;
		private Attribute(String value){
			this.value = value;
		}
		public String getValue(){
			return this.value;
		}
	}
	
	public static enum Operator {
		Equals(" = '$'"),
		Contains("like '%$%'"),
		StartWith("like '$%'"),
		EndWith("like '%$'"),
		LessThan("< '$'"),
		GreatThan("> '$'"),
		NotEqual("<> '$'"),
		And("and"),
		Or("or");
		private String value;
		private Operator(String value){
			this.value = value;
		}
		public String getValue(){
			return this.value;
		}
	}
}